<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true" label-width="76px">
        <el-form-item label="作业类别">
          <el-select
            v-model="searchParam.taskKind"
            placeholder="请选择作业类别"
            @change="changeTaskKind"
            clearable
          >
            <el-option label="全部" value="" />
            <el-option
              v-for="item in useDictionary('QmsTaskKind')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="作业类型">
          <el-select
            v-model="searchParam.sopTypeCode"
            placeholder="请选择作业类型"
            clearable
            :disabled="props.sopType !== ''"
          >
            <el-option
              v-for="item in typeList"
              :key="item.sopTypeCode"
              :label="item.sopTypeName"
              :value="item.sopTypeCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="作业名称">
          <el-input
            v-model="searchParam.standardName"
            placeholder="请输入作业名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">
            <i-ep-search />
            搜索
          </el-button>
          <el-button @click="reset">
            <i-ep-refresh />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()">
          <i-ep-plus />
          新增
        </el-button>
        <el-button
          type="danger"
          plain
          :disabled="ids.length === 0"
          @click="handleTableDelete()"
        >
          <i-ep-delete />
          删除
        </el-button>
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        :cell-style="{ 'text-align': 'center' }"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="作业编号"
          prop="standardCode"
          min-width="180"
          show-overflow-tooltip
        />
        <el-table-column
          label="作业名称"
          prop="standardName"
          min-width="200"
          show-overflow-tooltip
        />
        <el-table-column
          label="作业类别"
          align="center"
          prop="taskKind"
          min-width="120"
        >
          <template #default="scope">
            <el-tag
              :type="
                scope.row.taskKind == '1'
                  ? 'success'
                  : scope.row.taskKind == '2'
                  ? ''
                  : scope.row.taskKind == '3'
                  ? 'warning'
                  : 'info'
              "
              >{{ useDictionary("QmsTaskKind", scope.row.taskKind) }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          label="作业类型"
          prop="sopTypeName"
          min-width="180"
          show-overflow-tooltip
        />
        <el-table-column
          label="作业人员类型"
          prop="standardPersonnel"
          min-width="120"
          show-overflow-tooltip
        >
          <template #default="scope">
            <span>
              {{
                useDictionary("StandardPersonnel", scope.row.standardPersonnel)
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="工序"
          prop="processName"
          min-width="160"
          show-overflow-tooltip
        />
        <el-table-column
          label="产品"
          prop="productName"
          min-width="160"
          show-overflow-tooltip
        />
        <el-table-column
          label="编制时间"
          prop="standardCompileTime"
          min-width="160"
          show-overflow-tooltip
        />
        <el-table-column
          label="编制人"
          prop="standardCompilePersonnel"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="审核时间"
          prop="standardExamineTime"
          min-width="160"
          show-overflow-tooltip
        />
        <el-table-column
          label="审核人"
          prop="standardExaminePersonnel"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="批准时间"
          prop="standardRatifyTime"
          min-width="160"
          show-overflow-tooltip
        />
        <el-table-column
          label="批准人"
          prop="standardRatifyPersonnel"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="是否启用"
          align="center"
          prop="enabled"
          min-width="100"
        >
          <template #default="scope">
            <el-tag v-show="scope.row.enabled === 1" type="success">是</el-tag>
            <el-tag v-show="scope.row.enabled !== 1" type="info">否</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="备注"
          prop="remarks"
          min-width="200"
          show-overflow-tooltip
        />
        <el-table-column fixed="right" label="操作" width="280">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogAllDetail(scope.row)"
            >
              <i-ep-search />
              作业指导书
            </el-button>
            <!-- <el-button
              type="primary"
              size="small"
              link
              @click="openDialogDetail(scope.row)"
            >
              <i-ep-search />
              查看
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
            >
              <i-ep-edit />
              编辑
            </el-button> -->
            <el-button
              type="primary"
              size="small"
              link
              @click="jumpUrl(scope.row)"
            >
              <i-ep-tickets />
              作业项
            </el-button>
            <!-- <el-button
              type="primary"
              size="small"
              link
              @click="deleteItem(scope.row.id)"
            >
              <i-ep-delete />
              删除
            </el-button> -->
            <!-- <el-dropdown>
              <span class="el-dropdown-link">
                更多操作
                <el-icon class="el-icon--right">
                  <arrow-down />
                </el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="openDialogDetail(scope.row)"
                    >查看</el-dropdown-item
                  >
                  <el-dropdown-item @click="openDialog(scope.row)"
                    >编辑</el-dropdown-item
                  >
                  <el-dropdown-item @click="deleteItem(scope.row.id)"
                    >删除</el-dropdown-item
                  >
                </el-dropdown-menu>
              </template>
            </el-dropdown>

            <el-dropdown split-button type="primary">
              更多
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="openDialogDetail(scope.row)"
                    >查看</el-dropdown-item
                  >
                  <el-dropdown-item @click="openDialog(scope.row)"
                    >编辑</el-dropdown-item
                  >
                  <el-dropdown-item @click="deleteItem(scope.row.id)"
                    >删除</el-dropdown-item
                  >
                </el-dropdown-menu>
              </template>
            </el-dropdown> -->

            <el-dropdown class="ml-[12px] align-middle!">
              <span
                class="el-dropdown-link text-[12px] c-[#409eff] cursor-pointer"
              >
                更多
                <i-ep-arrow-down size="10" class="text-[10px]! ml-[3px]!" />
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="openDialogDetail(scope.row)"
                    >查看</el-dropdown-item
                  >
                  <el-dropdown-item @click="openDialog(scope.row)"
                    >编辑</el-dropdown-item
                  >
                  <el-dropdown-item
                    :disabled="scope.row.bind"
                    @click="deleteItem(scope.row.id)"
                    >删除</el-dropdown-item
                  >
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <qms-standard-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      :assetKind="assetKind"
      :sopType="sopType"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />

    <qms-standard-detail
      :visible="detailDialog.visible"
      :title="detailDialog.title"
      :data="detailDialog.data"
      :assetKind="assetKind"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="detailDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import { ArrowDown } from "@element-plus/icons-vue";
import { getStandardTypesApi } from "@/utils/commonData";
import request from "@/utils/request";
import router from "@/router";

defineOptions({
  name: "QmsSopStandard",
  inheritAttrs: false,
});

const props = defineProps({
  assetKind: {
    type: Number,
    default: null,
  },
  sopType: {
    type: String,
    default: "",
  },
});

const { assetKind, sopType } = toRefs(props);

// 请求参数
const requestApi = "/qms/qmssopstandard/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  standardCode: "",
  standardName: "",
  sopTypeId: "",
  taskKind: "",
  equipTypeId: "",
  brandId: "",
  assetKind: assetKind.value,
  sopTypeCode: sopType.value,
};

const deleteParam = {
  api: "/qms/qmssopstandard/delete",
  data: {
    assetKind: assetKind.value,
  },
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  searchParam.sopTypeCode = props.sopType;
  getTypeList("");
  getTableList();
});

// 查看弹窗所有信息
const detailDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查看弹窗 弹出
function openDialogAllDetail(roleObj?: any) {
  detailDialog.title = "作业指导书详情";
  getAllInfo(roleObj.id);
  detailDialog.readvisible = true;
}
// 查看弹窗 获取详情
async function getAllInfo(id: string) {
  request({
    url: `/qms/qmssopstandard/get/${id}`,
    method: "get",
    data: {
      assetKind: assetKind.value,
    },
  })
    .then((data) => {
      detailDialog.data = data.data;
      detailDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

// 编辑弹窗所有信息
const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/qms/qmssopstandard/get/${id}`,
    method: "get",
    data: {
      assetKind: assetKind.value,
    },
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开标准作业规程表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改作业规程";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增作业规程";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开标准作业规程表单弹窗 详情 */
function openDialogDetail(roleObj?: any) {
  addDialog.title = "作业规程详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  detailDialog.visible = value;
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
    detailDialog.data = {};
  }
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const { data } = await request({
      url: `/qms/qmssopstandard/deleteItem/${id}`,
      method: "post",
      data: {
        assetKind: assetKind.value,
      },
    });
    search();
  });
}

// 获取作业类型列表
const typeList = ref(<any>[]);
async function getTypeList(value: string) {
  const params = {
    taskKind: value,
    assetKind: props.assetKind,
  };
  const { data } = await request({
    url: "/qms/qmssoptype/getlist",
    method: "post",
    data: params,
  });
  typeList.value = data ?? [];
}

// 作业类别变更事件
function changeTaskKind(value: string) {
  typeList.value = [];
  searchParam.value.sopTypeId = "";
  getTypeList(value);
}

// 作业项页面跳转
function jumpUrl(row: any) {
  router.push({
    name: "QMSQMSSOPITEMS",
    query: { id: row.id },
  });
}
</script>
<style lang="scss"></style>
